<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>test</title>
</head>
<body>
</body>
<script>
    
    var plusOne = function(digits) {
        const len = digits.length;
        if(len==1){
            if(digits[0] != 9){
                digits[0]++;
                return digits;
            }else{
                digits[0]=0;
                digits.unshift(1);
                return digits;
            }
        }
        for(let i=len-1; i>=0; i--){
            if(digits[i] != 9){
                digits[i]++;
                return digits;
            }else{
                digits[i] = 0;
                while(i--){
                    if(digits[i] != 9){
                        digits[i]++;
                        return digits;
                    }else{
                        digits[i]=0;
                    }
                    if(i==0){
                        digits.unshift(1);
                        return digits;
                    }
                }
            }
        }
        
    };
    let arr = [9];
    let result = plusOne(arr);
    console.log(result);
    

</script>
</html>